package com.sbcb.mongodb.controller;

import com.google.gson.Gson;
import com.sbcb.mongodb.service.NativeMongoDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/nativemongodb")
public class NativeMongoDBController {
    @Autowired
    NativeMongoDBService nativeMongoDBService;

    //http://localhost:8080/nativemongodb/createCollection
    @GetMapping("createCollection")
    public String createCollection(){
        String collectionName = nativeMongoDBService.createCollection();
        return "collection is created: "+collectionName;
    }

    //http://localhost:8080/nativemongodb/documentcount
    @GetMapping("documentcount")
    public long documentcount(){
        return nativeMongoDBService.documentAmount();
    }

    //http://localhost:8080/nativemongodb/collectionlist
    @GetMapping("collectionlist")
    public String collectionlist(){
        Gson gson = new Gson();
        return gson.toJson(nativeMongoDBService.listCollections());
    }

    //http://localhost:8080/nativemongodb/createIndex
    @GetMapping("createIndex")
    public String createIndex(){
        return nativeMongoDBService.createIndex();
    }

    //http://localhost:8080/nativemongodb/findDocuments
    @GetMapping("findDocuments")
    public String findDocuments(){
        Gson gson = new Gson();
        return gson.toJson(nativeMongoDBService.findOrderList());
    }

    //http://localhost:8080/nativemongodb/explain
    @GetMapping("explain")
    public String explain(){
        Gson gson = new Gson();
        return gson.toJson(nativeMongoDBService.executeExplain());
    }
}
